/*
 * Buttonizer.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.colorschemes;

import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;

public class Buttonizer {

	public final static int GER_DRAWABLE = 0;
	public final static int GER_COLOR_DRAWABLE = 1;
	public final static int UK_DRAWABLE = 2;
	public final static int UK_COLOR_DRAWABLE = 3;
	public final static int GERUK_DRAWABLE = 4;
	public final static int GERUK_COLOR_DRAWABLE = 5;
	public final static int ERASE_DRAWABLE = 6;
	private final static int DRAWABLE_SIZE = 7;

	private final static float SCALING_X = 0.5f;
	private final static float SCALING_Y = 0.5f;

	private final static int INSET_HORIZONTAL = 5;
	private final static int INSET_VERTICAL = 10;

	private Drawable[] drawables = new Drawable[DRAWABLE_SIZE];
	private Resources res;

	public final static int STYLE_DEFAULT = 0;
	public final static int STYLE_IPHONE = 1;

	protected int buttonDefaultDrawableRes = R.drawable.btn_glass_default_selector;
	protected int buttonIphoneDrawableRes = R.drawable.btn_iphone_default_selector;
	protected int buttonStyle;

	public Buttonizer(int style) {
		res = LearningWordsApplication.get().getResources();
		if (style == STYLE_DEFAULT)
			buttonStyle = buttonDefaultDrawableRes;
		else
			buttonStyle = buttonIphoneDrawableRes;
		createDrawables();
	}

	private Drawable create(Resources res, int resId, float scalingX,
			float scalingY, boolean doScale, boolean doInset) {

		Drawable contentDrawable = res.getDrawable(resId);
		// ScaleDrawable scaleDrawable = new ScaleDrawable(contentDrawable,
		// Gravity.CENTER,
		// 100, 200);
		Drawable scaleDrawable;
		if ( doScale )
			scaleDrawable = scaleDrawable(contentDrawable, SCALING_X,
				SCALING_Y);
		else
			scaleDrawable = contentDrawable;
		Drawable insetDrawable;
		if ( doInset ) {
				insetDrawable = new InsetDrawable(scaleDrawable,
				INSET_HORIZONTAL, INSET_VERTICAL, INSET_HORIZONTAL,
				INSET_VERTICAL);
		} 
		else
			insetDrawable = scaleDrawable;
		Drawable buttonDrawable = res.getDrawable(buttonStyle);
		Drawable[] layeredDrawables = new Drawable[] { buttonDrawable,
				insetDrawable };
		LayerDrawable layerDrawable = new LayerDrawable(layeredDrawables);
		return layerDrawable;
	}

	private void createDrawables() {
		drawables[GER_DRAWABLE] = create(res, R.drawable.flag_ger_64_32_grey,
				SCALING_X, SCALING_Y, true, true );
		drawables[GER_COLOR_DRAWABLE] = create(res, R.drawable.flag_ger_64_32,
				SCALING_X, SCALING_Y, true, true);
		drawables[UK_DRAWABLE] = create(res, R.drawable.flag_uk_64_32_grey,
				SCALING_X, SCALING_Y, true, true);
		drawables[UK_COLOR_DRAWABLE] = create(res, R.drawable.flag_uk_64_32,
				SCALING_X, SCALING_Y, true, true);
		drawables[GERUK_DRAWABLE] = create(res,
				R.drawable.flag_geruk_64_32_grey, SCALING_X, SCALING_Y, true, true);
		drawables[GERUK_COLOR_DRAWABLE] = create(res,
				R.drawable.flag_geruk_64_32, SCALING_X, SCALING_Y, true, true);
		drawables[ERASE_DRAWABLE] = create(res,
				android.R.drawable.ic_input_delete, 2.0f, 2.0f, false, false);
	}

	public Drawable getDrawable(int id) {
		if (id < 0 || id > DRAWABLE_SIZE - 1)
			throw new IndexOutOfBoundsException();
		return drawables[id];
	}

	public Drawable getDrawable(int id, boolean grey) {
		if (id < 0 || id > DRAWABLE_SIZE - 1)
			throw new IndexOutOfBoundsException();
		if (grey)
			return drawables[id + 1];
		else
			return drawables[id];
	}

	private Drawable scaleDrawable(Drawable initial, float xChange,
			float yChange) {
		if (!(initial instanceof BitmapDrawable)) {
			Log.d("learningwords", "Initial is no BitmapDrawable: its a "
					+ initial.getClass());
			return initial;
		}

		Bitmap bm = ((BitmapDrawable) initial).getBitmap();
		Matrix matrix = new Matrix();
		matrix.postScale(xChange, xChange);
		Bitmap nbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
				bm.getHeight(), matrix, true);
		BitmapDrawable out = new BitmapDrawable(nbm);
		return out;
	}
}
